<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var combine = function (n, k) {
      let ans = [];
      function dfs(start, path) {
        if (path.length === k) {
          ans.push(path);
          return;
        }

        for (let i = start; i <= n; i++) {
          dfs(i + 1, [...path, i]);
        }
      }
      dfs(1, []);
      return ans;
    };
    console.log(combine(4, 2))
  </script>
</body>

</html>